<!DOCTYPE html>
<html>

<head lang="en">
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, user-scalable=no, initial-scale=1.0" />
    <meta content="yes" name="apple-mobile-web-app-capable">
    <meta content="black" name="apple-mobile-web-app-status-bar-style">
    <meta content="telephone=no" name="format-detection">
    <title>传智云购-搜索中心</title>
    <link type="image/x-icon" rel="shortcut icon" href="images/favicon.ico">
    <link rel="stylesheet" href="res/mui/css/mui.min.css" />
    <link rel="stylesheet" href="res/fontAwesome/css/font-awesome.min.css" />
    <link rel="stylesheet" href="css/common.css" />
    <link rel="stylesheet" href="css/product.css" />
    <style>
        * { touch-action: none; }
        .active{
            background: orange;
        }
    </style>
</head>

<body>
    <div class="cz_layout">
        <header class="cz_topBar">
            <a href="javascript:history.back();" class="icon_back fa fa-arrow-left"></a>
            <span class="title">商品详情</span>
            <a href="index.html" class="icon_home fa fa-home"></a>
        </header>
        <div class="cz_container ">
            <div class="mui-scroll-wrapper">
                <div class="mui-scroll" id="detail">
                    <div class="cz_banner mui-slider">
                        <div class="mui-slider-group mui-slider-loop">
                            <div class="mui-slider-item mui-slider-item-duplicate">
                                <a href="#"><img src="images/product.jpg"></a>
                            </div>
                            <div class="mui-slider-item mui-active">
                                <a href="#"><img src="images/product.jpg"></a>
                            </div>
                            <div class="mui-slider-item mui-active">
                                <a href="#"><img src="images/product.jpg"></a>
                            </div>
                            <div class="mui-slider-item mui-slider-item-duplicate">
                                <a href="#"><img src="images/product.jpg"></a>
                            </div>
                        </div>
                        <div class="mui-slider-indicator">
                            <div class="mui-indicator mui-active"></div>
                            <div class="mui-indicator"></div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
    <div class="p_option">
        <a href="#" class="btn_cart fa fa-shopping-cart"></a>
        <a href="javascript:;" class="btn_addCart">加入购物车</a>
        <a href="#" class="btn_pay">立即购买</a>
    </div>
    <script src="res/mui/js/mui.min.js"></script>
    <script src="res/mui/js/mui.lazyload.js"></script>
    <script src="res/mui/js/mui.lazyload.img.js"></script>
    <script src="/assets/vendors/jquery/jquery.min.js"></script>
    <script src="/assets/vendors/template-web.js"></script>
    <script src="/assets/vendors/layer/layer.js"></script>
    <!-- 模板引擎 -->
    <script type="text/template" id="tpl">

        <div class="p_title">
            {{proName}}
        </div>
        <div class="p_price">
            <span class="fold"><span>{{discount}}</span> 折</span>
            价格：<strong class="present">¥
                {{price}}</strong>
            <del class="original">¥{{oldPrice}}</del>
        </div>
        <div class="p_size">
            尺码：
            {{each size value}}
            <span class="btn_size">{{value}}</span>
            {{/each}}
        </div>
        <div class="p_number">
            数量：
            <span class="jian btn-num">-</span>
            <input type="tel" value="1" readonly/>
            <span class="jia btn-num">+</span>
            剩余：<span class="repertory">{{num}}</span> 件
        </div>
        <div class="p_detail">

        </div>
    </script>
    <script>
        //获取产品id
        var id = location.search.slice(1);
        // 定义一个对象接收处理好的数据
        var qs = {};
        //按照 & 拆分
        var arr = id.split('&');
        //按照 = 拆分
        $(arr).each(function (index, element) {
            var tmp = element.split('=');
            qs[tmp[0]] = tmp[1];

        });
        // console.log(qs);

        $.ajax({
            url: '/api/product/queryProductDetail',
            data: {
                id: qs.id
            },
            success: function (msg) {
                msg.size = msg.size.split('-');
                // 折扣
                msg.discount = (msg.price / msg.oldPrice * 10).toFixed(2);
                //模板引擎
                var strHtml = template('tpl', msg);
                $('.mui-scroll').append(strHtml);
                // console.log(msg.discount)
            }

        });

        //当前尺码选中状态
        $('#detail').on('click', '.btn_size', function () {
            $(this).addClass('active').siblings().removeClass('active');
        })
        //商品数量加减
        $('#detail').on('click', '.btn-num', function () {
            var input = $(this).siblings('input');
            // num输入框的值
            // num 是字符串  需要转为数字
            var num = Number(input.val());
            if ($(this).is('.jia')) {

                var repertory = $('span.repertory').text()
                // console.log(repertory);
                // console.log('num: ' + num);
                //判断num值 如果num>库存  提示库存不足
                // if (num >= repertory) {
                //     num = repertory;
                // };
                if (num >= repertory) {
                    mui.toast('库存不足啦');
                    return;
                };
                input.val(++num);
                return;
            }
            if (num <= 1) {
                mui.toast('最少选择一件商品哟');
                return;
            };
            input.val(--num);
        })
        $('.btn_addCart').click(function () {
            //判断用户是否选择型号

            var num = $('.repertory').val();
            var size = $('span.active').text();
            if (!size) {
                mui.alert('请选择尺码');
                return;
            }
            $.ajax({
                url: '/api/cart/addCart',
                type: 'post',
                data: {
                    productId: qs.id,
                    num: num,
                    size: size
                },
                success: function (msg) {
                    // console.log(msg);
                    //判断用户是否登录
                    if (msg.error) {
                        mui.alert('请先登录', function () {
                            location.href = '/user/login.html?url=' + location.href;
                        })
                        return;
                    }
                    if (msg.success) {
                        layer.msg('添加购物车成功', {
                                shift: 5,
                                time: 700
                            },
                            function () {
                                location.href = 'cart.html';
                            })
                    }
                }
            });
        });
        // ----------------  MUI  ----------------
        mui(function () {
            mui('.mui-slider').slider({
                interval: 2000 //自动轮播周期，若为0则不自动播放，默认为0；
            });
            mui('.mui-scroll-wrapper').scroll({
                scrollY: true, //是否竖向滚动
                scrollX: false, //是否横向滚动
                startX: 0, //初始化时滚动至x
                startY: 0, //初始化时滚动至y
                indicators: false, //是否显示滚动条
                deceleration: 0.0006, //阻尼系数,系数越小滑动越灵敏
                bounce: true, //是否启用回弹
            });

            // mui.init({
            //     pullRefresh: {
            //         container: '.mui-scroll-wrapper',
            //         down: {
            //             callback: function () {
            //                 setTimeout(function () {
            //                     mui('.mui-scroll-wrapper').pullRefresh().endPulldownToRefresh(); //refresh completed
            //                 }, 1000);
            //             }
            //         }
            //     }
            // });
            mui('.p_detail').imageLazyload({
                placeholder: '../images/user.jpg'
            });
        });
    </script>

</body>

</html>